package com.dkd.day20250616;

public class SolutionIsBalanced_Solution {
    public static void main(String[] args) {


    }
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param pRoot TreeNode类
     * @return bool布尔型
     */
    boolean res = true;
    public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        maxDepth(pRoot);
        return res;
    }

    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        //返回左右节点中的最大深度
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        if (left == -1) return -1;
        if (right == -1) return -1;
        if (Math.abs(left - right) > 1){
            res = false;
            return -1;
        };
        return Math.max(left, right) + 1;
    }
}

